const mongoose = require('mongoose')
const Schema = mongoose.Schema
const {ObjectId,Mixed} = Schema.Types

const MovieSchema = new Schema({
    doubanId: {
        unique: true,
        required: true,
        type: String
    },

    category: [{
        type: ObjectId,
        ref: 'Category'
    }],

    rate: Number,
    title: String,
    summary: String,
    video: String,
    poster: String,
    cover: String,



    rawTitle:String,
    movieTypes: [String],
    pubdate: Mixed,
    year: Number,

    tags: Array,
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt: {
            type: Date,
            default: Date.now()
        }
    }
})

MovieSchema.pre("save", function (next){
    if (this.isNew) {
        this.meta.createdAt = this.meta.updatedAt = Date.now()
    } else {
        this.meta.updatedAt = Date.now()
    }
    next()
})



mongoose.model("Movie", MovieSchema)